﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseAccess;
using Liguo.AppCode;
namespace Liguo.Store
{
    public partial class MyOrder : System.Web.UI.Page
    {

        //string userid = "91";
        User user;
        string userid;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["User"] == null)
            {
                Response.Redirect("Port.aspx");
            }
            else
            {
                user = (User)Session["User"];
                userid = Convert.ToString(user.UserID);

                if (!IsPostBack)
                {

                    BindOrderList(10);
                }
                DataSet ds3 = new DataSet();


                for (int i = 0; i < DataList1.Items.Count; i++)
                {
                    string Code = ((Label)this.DataList1.Items[i].FindControl("StatusLabel")).Text;
                    string sql2 = "select Status from OrderStatus where ID=" + Code;
                    SqlConnection conn1 = new SqlConnection("server=localhost;database=liguo;uid=sa;pwd=sql");
                    SqlCommand cmd1 = new SqlCommand(sql2, conn1);
                    SqlDataAdapter da = new SqlDataAdapter(cmd1);
                    da.Fill(ds3, "OrderStatus");
                    ((Label)this.DataList1.Items[i].FindControl("StatusLabel")).Text = ds3.Tables[0].Rows[0]["Status"].ToString();
                }
            }



        }
        private void BindOrderList(int length)
        {
            DataSet ds = new DataSet();

            string sql = "select * from OrderInfo where UserID=" + userid;
            SqlConnection conn1 = new SqlConnection("server=localhost;database=liguo;uid=sa;pwd=sql");
            conn1.Open();
            SqlCommand cmd1 = new SqlCommand(sql, conn1);
            SqlDataAdapter da = new SqlDataAdapter(cmd1);
            da.Fill(ds, "OrderInfo");
            DataTable dtOrderList = ds.Tables["OrderInfo"]; //获取数据
            string strLength;
            for (int i = 0; i < dtOrderList.Rows.Count; i++)
            {

                strLength = dtOrderList.Rows[i]["Time"].ToString();

                if (strLength.Length > length)
                {

                    dtOrderList.Rows[i]["Time"] = (Object)strLength.Substring(0, length);

                }

            }
            this.txtgo.Text = "";

            int curpage = Convert.ToInt32(this.labpage.Text);

            PagedDataSource ps = new PagedDataSource();   //定义PagedDataSource对象

            ps.DataSource = dtOrderList.DefaultView;  //取出数据到datatable中，赋给PagedDataSource对象
            ps.AllowPaging = true;  //是否可以分页

            ps.PageSize = 16; //显示的数量

            ps.CurrentPageIndex = curpage - 1; //取得当前页的页码，PagedDataSource的CurrentPageIndex是从0开始

            this.linkbtnone.Enabled = true;

            this.linkbtnpre.Enabled = true;

            this.linkbtnnext.Enabled = true;

            this.linkbtnlast.Enabled = true;


            if (curpage == 1)
            {

                this.linkbtnone.Enabled = false;

                this.linkbtnpre.Enabled = false;

            }

            if (curpage == ps.PageCount)
            {

                this.linkbtnnext.Enabled = false;

                this.linkbtnlast.Enabled = false;

            }

            this.labtp.Text = Convert.ToString(ps.PageCount);  //显示分页数量

            this.DataList1.DataSource = ps;   //绑定DataList控件

            this.DataList1.DataKeyField = "Time";

            this.DataList1.DataBind();
        }

        //第一页

        protected void linkbtnone_Click(object sender, EventArgs e)
        {

            this.labpage.Text = "1";

            BindOrderList(10);

        }

        //上一页

        protected void linkbtnpre_Click(object sender, EventArgs e)
        {

            this.labpage.Text = Convert.ToString(Convert.ToInt32(this.labpage.Text) - 1);

            BindOrderList(10);

        }

        //下一页

        protected void linkbtnnext_Click(object sender, EventArgs e)
        {

            this.labpage.Text = Convert.ToString(Convert.ToInt32(this.labpage.Text) + 1);

            BindOrderList(10);

        }

        //最后一页

        protected void linkbtnlast_Click(object sender, EventArgs e)
        {

            this.labpage.Text = this.labtp.Text;

            BindOrderList(10);

        }

        //跳转按钮

        protected void BtnGo_Click(object sender, EventArgs e)
        {

            this.labpage.Text = this.txtgo.Text;

            BindOrderList(10);

        }



    }



}
